package com.peng.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.peng.edu.entity.Course;
import com.peng.edu.entity.Teacher;
import com.peng.edu.service.CourseService;
import com.peng.edu.service.TeacherService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/edu/indexFront")
@CrossOrigin
public class IndexFrontController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    @GetMapping("/index")
    @Cacheable(value = "index" ,key = "'CourseAndTeacher'")
    public Result index(){
        LambdaQueryWrapper<Teacher> teacherQueryWrapper = new LambdaQueryWrapper<>();
        teacherQueryWrapper.orderByDesc(Teacher::getGmtCreate);
        teacherQueryWrapper.last("limit 4");
        List<Teacher> teacherList = teacherService.list(teacherQueryWrapper);

        LambdaQueryWrapper<Course> courseQueryWrapper = new LambdaQueryWrapper<>();
        courseQueryWrapper.orderByDesc(Course::getGmtCreate);
        courseQueryWrapper.last("limit 8");
        List<Course> courseList = courseService.list(courseQueryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("teacherList",teacherList);
        map.put("courseList",courseList);
        return Result.ok().data(map);
    }
}
